﻿using System;
using HigLabo.Net.Extensions;

namespace HigLabo.Net.WindowsLive
{
    /// <summary>
    /// 
    /// </summary>
    public class User : ResponseObject
    {
        /// <summary>
        /// 
        /// </summary>
        public String ID { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public String Name { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public String FirstName { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public String LastName { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public String Link { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public Int32 BirthDay { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public Int32 BirthMonth { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public Int32 BirthYear { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public DateTime? BirthDate { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public String Gender { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public String Locale { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public String UserName { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public String ThirdPartyID { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public Double? TimeZone { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public DateTimeOffset? UpdatedTime { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public Boolean? Verified { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public String Bio { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public UserWork Work { get; private set; }
        /// <summary>
        /// 
        /// </summary>
        public UserEmails Emails { get;private set; }
        /// <summary>
        /// 
        /// </summary>
        public UserAddresses Addresses { get; private set; }
        /// <summary>
        /// 
        /// </summary>
        public UserPhones Phones { get; private set; }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="jsonText"></param>
        public User(String jsonText)
        {
            this.SetProperty(jsonText);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="jsonText"></param>
        public override void SetProperty(String jsonText)
        {
            var d = this.SetData(jsonText);
            this.ID = d.ToString("id");
            this.Name = d.ToString("name");
            this.FirstName = d.ToString("first_name");
            this.LastName = d.ToString("last_name");
            this.Link = d.ToString("link");

            this.BirthDay = d.ToInt32("birth_day") ?? 0;
            this.BirthMonth = d.ToInt32("birth_month") ?? 0;
            this.BirthYear = d.ToInt32("birth_year") ?? 0;

            DateTime dtime;
            if (DateTime.TryParse(String.Format("{0}/{1}/{2}", this.BirthYear, this.BirthMonth, this.BirthDay), out dtime) == true)
            {
                this.BirthDate = dtime;
            }

            if (d.ContainsKey("work") == true)
            {
                this.Work = new UserWork(d.ToString("work"));
            }
            this.Gender = d.ToString("gender");
            if (d.ContainsKey("emails") == true)
            {
                this.Emails = new UserEmails(d.ToString("emails"));
            }
            if (d.ContainsKey("addresses") == true)
            {
                this.Addresses = new UserAddresses(d.ToString("addresses"));
            }
            //Phones
            this.Locale = d.ToString("locale");
            this.UpdatedTime = d.ToDateTimeOffset("updated_time");
        }
    }
}
